//
//  UIImage+PYUploadImageByQiniu.m
//  catuncel
//
//  Created by Jim on 2017/12/6.
//  Copyright © 2017年 Jim. All rights reserved.
//

#import "UIImage+PYUploadImageByQiniu.h"

#import "UIImage+PYImageManager.h"

#import <QiniuSDK.h>

@implementation UIImage (PYUploadImageByQiniu)

- (void)uploadImageByQiniuWithoutCompress:(NSString *)originalKey token:(NSString *)token compress:(PYUploadImageByQiniuCompletion)completion
{
    QNUploadOption *option = [[QNUploadOption alloc] initWithProgressHandler:^(NSString *key, float percent) {
        
        NSLog(@"image_percent_thumbnail = %f", percent);
    }];

    QNUploadManager *manager = [QNUploadManager new];

    NSData *data = UIImageJPEGRepresentation(self, 1.f);

    [manager putData:data key:originalKey token:token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
        
        NSLog(@"info = %@", info);
        
        NSLog(@"resp = %@", resp);
        
        if (completion) {
            
            completion(originalKey, nil);
        }
        
    } option:option];
}

- (void)uploadImageByQiniuAndCompress:(CGFloat)kb completion:(PYUploadImageByQiniuCompletion)completion
{
    NSString *URLString = [NSString stringWithFormat:@"%@/%@/%@/png", PY_GET_Header, PY_URI_ImageUploadToken, [NSString valueByKey:kCurrentUserID]];

    [PYNetworking networkingWithURLString:URLString parameters:nil requestType:PY_GET success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSString *token = responseObject[@"data"][@"token"];
        
        NSString *originalKey = responseObject[@"data"][@"originalKey"];
        
        if (token) {
            
            //1.创建队列组
            dispatch_group_t group = dispatch_group_create();
            //2.创建队列
            dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
            
            //3.多次使用队列组的方法执行任务, 只有异步方法
            
            __block NSData *data;
            
            dispatch_group_async(group, queue, ^{
                
                if (kb > 0) {
                    
                    data = [self compressToMaxDataSizeKBytes:kb];
                } else {
                    
                    data = UIImageJPEGRepresentation(self, 1.f);
                }
            });
            
            
            //4.都完成后会自动通知
            dispatch_group_notify(group, dispatch_get_main_queue(), ^{
                NSLog(@"完成 - %@", [NSThread currentThread]);
                
                UIImage *thumbnailImage = [UIImage imageWithData:data];
                
                NSLog(@"image_data_thumbnail = %ld", data.length / 1024);
                
                NSLog(@"image_size_thumbnail = %@", NSStringFromCGSize(thumbnailImage.size));
                
                NSString *dpi = [NSString stringWithFormat:@"%f", thumbnailImage.size.height / thumbnailImage.size.width];
                
                NSLog(@"thumbnailImage.dpi = %@", dpi);
                
                QNUploadOption *option = [[QNUploadOption alloc] initWithProgressHandler:^(NSString *key, float percent) {
                    
                    NSLog(@"image_percent_thumbnail = %f", percent);
                }];
                
                QNUploadManager *manager = [QNUploadManager new];
                
                [manager putData:data key:originalKey token:token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
                    
                    NSLog(@"info = %@", info);
                    
                    NSLog(@"resp = %@", resp);
                    
                    if (completion) {
                        
                        completion(originalKey, nil);
                    }
                    
                } option:option];
                
            });
            
            return ;
        }
        
        if (completion) {
            NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:400 userInfo:@{@"key":@"服务器异常"}];
            
            completion(nil, error);
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        if (completion) {
            
            completion(nil, error);
            
        }
        
    }];
}

@end
